package com.synjones.pay.web.biz.transfer.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.synjones.core.util.AmtHelper;
import com.synjones.core.util.Charsets;
import com.synjones.core.util.StringUtil;
import com.synjones.pay.model.context.StatusInfo;
import com.synjones.pay.model.transfer.TransferReqBean;
import com.synjones.pay.model.transfer.TransferRespBean;
import com.synjones.pay.web.biz.client.RestFulClient;
import static com.synjones.pay.web.biz.common.PageParams.TICKET;
import com.synjones.pay.web.biz.common.SystemCfg;
import com.synjones.pay.web.biz.common.WxResponseBean;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 *
 * @author sxj
 */
@Service("transferService")
public class TransferServiceImpl implements ITransferService {

    private static final Logger logger = LoggerFactory.getLogger(TransferServiceImpl.class);

    /**
     * *
     * TODO 验签未完成
     *
     * @param map
     * @return
     */
    @Override
    public boolean verifyData(Map<String, String> map) {
        //TODO
        //////////////////
        //1、校验应用合法性
        //2、请求者id
        //3、请求时间
        return true;
    }

    @Override
    public WxResponseBean getEcardInfoByReqId(String ticket) {
        Map<String, String> map = new HashMap<>();
        map.put(TICKET, ticket);
//        String httpPost = HttpClientUtils.httpPost(SystemCfg.ECARD_URL, map, Charsets.UTF_8.name());
         String httpPost = "{\"errCode\":\"0000\",\"errMsg\":null,\"tipMsg\":null,\"data\":{\"cardInfos\":[{\"eaccInfos\":[{\"name\":\"test2\",\"type\":\"001\",\"balance\":\"3911.0\"},{\"name\":\"test1\",\"type\":\"000\",\"balance\":\"17000.0\"}],\"account\":\"101987\",\"name\":\"田少英\",\"unsettle_amount\":\"-128199.0\",\"db_balance\":\"173999.0\",\"lostflag\":\"0\",\"freezeflag\":\"0\",\"idflag\":\"0\",\"barflag\":null,\"createdate\":\"2015-04-12T00:00:00\",\"expdate\":\"20350627\",\"cardtype\":\"800\",\"cardname\":\"正式卡\",\"bankacc\":\"\",\"sno\":\"03500326\",\"phone\":\"\",\"certtype\":\"001\",\"cert\":\"140103680911425\",\"autotrans_limite\":\"1000.0\",\"autotrans_amt\":\"2000.0\",\"autotrans_flag\":\"0\",\"mscard\":\"0\",\"scard_num\":\"0\"},{\"eaccInfos\":null,\"account\":\"6046\",\"name\":\"田少英\",\"unsettle_amount\":\"-280.0\",\"db_balance\":\"280.0\",\"lostflag\":\"1\",\"freezeflag\":\"0\",\"idflag\":\"0\",\"barflag\":null,\"createdate\":\"2002-08-28T00:00:00\",\"expdate\":\"20200716\",\"cardtype\":\"800\",\"cardname\":\"正式卡\",\"bankacc\":\"9558803602124739855\",\"sno\":\"03500326\",\"phone\":\"\",\"certtype\":\"001\",\"cert\":\"140103680911425\",\"autotrans_limite\":\"2000.0\",\"autotrans_amt\":\"5000.0\",\"autotrans_flag\":\"2\",\"mscard\":\"0\",\"scard_num\":\"0\"},{\"eaccInfos\":null,\"account\":\"60461111\",\"name\":\"田少英\",\"unsettle_amount\":\"109220.0\",\"db_balance\":\"280.0\",\"lostflag\":\"0\",\"freezeflag\":\"0\",\"idflag\":\"0\",\"barflag\":null,\"createdate\":\"2002-08-28T00:00:00\",\"expdate\":\"20200716\",\"cardtype\":\"800\",\"cardname\":\"正式卡\",\"bankacc\":\"9558803602124739855\",\"sno\":\"03500326\",\"phone\":\"\",\"certtype\":\"001\",\"cert\":\"140103680911425\",\"autotrans_limite\":\"2000.0\",\"autotrans_amt\":\"5000.0\",\"autotrans_flag\":\"2\",\"mscard\":\"0\",\"scard_num\":\"0\"},{\"eaccInfos\":null,\"account\":\"60462222\",\"name\":\"田少英\",\"unsettle_amount\":\"44720.0\",\"db_balance\":\"280.0\",\"lostflag\":\"0\",\"freezeflag\":\"0\",\"idflag\":\"0\",\"barflag\":null,\"createdate\":\"2002-08-28T00:00:00\",\"expdate\":\"20200716\",\"cardtype\":\"800\",\"cardname\":\"正式卡\",\"bankacc\":\"9558803602124739855\",\"sno\":\"03500326\",\"phone\":\"\",\"certtype\":\"001\",\"cert\":\"140103680911425\",\"autotrans_limite\":\"2000.0\",\"autotrans_amt\":\"5000.0\",\"autotrans_flag\":\"2\",\"mscard\":\"0\",\"scard_num\":\"0\"},{\"eaccInfos\":null,\"account\":\"60463333\",\"name\":\"田少英\",\"unsettle_amount\":\"131720.0\",\"db_balance\":\"280.0\",\"lostflag\":\"0\",\"freezeflag\":\"0\",\"idflag\":\"0\",\"barflag\":null,\"createdate\":\"2002-08-28T00:00:00\",\"expdate\":\"20200716\",\"cardtype\":\"800\",\"cardname\":\"正式卡\",\"bankacc\":\"9558803602124739855\",\"sno\":\"03500326\",\"phone\":\"\",\"certtype\":\"001\",\"cert\":\"140103680911425\",\"autotrans_limite\":\"2000.0\",\"autotrans_amt\":\"5000.0\",\"autotrans_flag\":\"2\",\"mscard\":\"0\",\"scard_num\":\"0\"},{\"eaccInfos\":null,\"account\":\"60464444\",\"name\":\"田少英\",\"unsettle_amount\":\"-280.0\",\"db_balance\":\"280.0\",\"lostflag\":\"0\",\"freezeflag\":\"0\",\"idflag\":\"0\",\"barflag\":null,\"createdate\":\"2002-08-28T00:00:00\",\"expdate\":\"20200716\",\"cardtype\":\"800\",\"cardname\":\"正式卡\",\"bankacc\":\"9558803602124739855\",\"sno\":\"03500326\",\"phone\":\"\",\"certtype\":\"001\",\"cert\":\"140103680911425\",\"autotrans_limite\":\"2000.0\",\"autotrans_amt\":\"5000.0\",\"autotrans_flag\":\"2\",\"mscard\":\"0\",\"scard_num\":\"0\"},{\"eaccInfos\":null,\"account\":\"101381\",\"name\":\"田少英\",\"unsettle_amount\":\"0.0\",\"db_balance\":\"0.0\",\"lostflag\":\"0\",\"freezeflag\":\"0\",\"idflag\":\"0\",\"barflag\":null,\"createdate\":\"2015-04-12T00:00:00\",\"expdate\":\"20350423\",\"cardtype\":\"800\",\"cardname\":\"正式卡\",\"bankacc\":\"\",\"sno\":\"03500326\",\"phone\":\"\",\"certtype\":\"001\",\"cert\":\"140103680911425\",\"autotrans_limite\":\"2000.0\",\"autotrans_amt\":\"5000.0\",\"autotrans_flag\":\"1\",\"mscard\":\"0\",\"scard_num\":\"0\"},{\"eaccInfos\":[{\"name\":\"test1\",\"type\":\"000\",\"balance\":\"50000.0\"},{\"name\":\"test6\",\"type\":\"005\",\"balance\":\"0.0\"},{\"name\":\"test2\",\"type\":\"001\",\"balance\":\"0.0\"}],\"account\":\"30\",\"name\":\"田少英\",\"unsettle_amount\":\"-106435.0\",\"db_balance\":\"106435.0\",\"lostflag\":\"0\",\"freezeflag\":\"1\",\"idflag\":\"0\",\"barflag\":null,\"createdate\":\"2015-04-12T00:00:00\",\"expdate\":\"20351123\",\"cardtype\":\"800\",\"cardname\":\"正式卡\",\"bankacc\":\"\",\"sno\":\"03500326\",\"phone\":\"\",\"certtype\":\"001\",\"cert\":\"140103680911425\",\"autotrans_limite\":\"2000.0\",\"autotrans_amt\":\"5000.0\",\"autotrans_flag\":\"1\",\"mscard\":\"0\",\"scard_num\":\"0\"},{\"eaccInfos\":null,\"account\":\"10138222\",\"name\":\"田少英\",\"unsettle_amount\":\"0.0\",\"db_balance\":\"0.0\",\"lostflag\":\"0\",\"freezeflag\":\"0\",\"idflag\":\"0\",\"barflag\":null,\"createdate\":\"2015-04-12T00:00:00\",\"expdate\":\"20350423\",\"cardtype\":\"800\",\"cardname\":\"正式卡\",\"bankacc\":\"\",\"sno\":\"03500326\",\"phone\":\"\",\"certtype\":\"001\",\"cert\":\"140103680911425\",\"autotrans_limite\":\"2000.0\",\"autotrans_amt\":\"5000.0\",\"autotrans_flag\":\"1\",\"mscard\":\"0\",\"scard_num\":\"0\"},{\"eaccInfos\":null,\"account\":\"10138333\",\"name\":\"田少英\",\"unsettle_amount\":\"0.0\",\"db_balance\":\"0.0\",\"lostflag\":\"0\",\"freezeflag\":\"0\",\"idflag\":\"0\",\"barflag\":null,\"createdate\":\"2015-04-12T00:00:00\",\"expdate\":\"20350423\",\"cardtype\":\"800\",\"cardname\":\"正式卡\",\"bankacc\":\"\",\"sno\":\"03500326\",\"phone\":\"\",\"certtype\":\"001\",\"cert\":\"140103680911425\",\"autotrans_limite\":\"2000.0\",\"autotrans_amt\":\"5000.0\",\"autotrans_flag\":\"1\",\"mscard\":\"0\",\"scard_num\":\"0\"}],\"userInfo\":{\"account\":\"03500326\",\"sex\":\"2\",\"name\":\"田少英\",\"schoolCode\":\"wx\",\"schoolName\":\"微校园\"}},\"time\":null}";
        logger.info("转账返回用户信息：\r\n{}", httpPost);
        WxResponseBean respBean = JSON.parseObject(httpPost, WxResponseBean.class);
        return WxResponseBean.assemErrorWxRespBean(respBean);//httpost 异常时
    }

    @Override
    public TransferRespBean transferToCard(TransferReqBean trb) {
        //需要将amount转换为以分为单位
        trb = amountYuanToPoint(trb);
        String jsonParam = JSON.toJSONString(trb, SerializerFeature.WriteMapNullValue, SerializerFeature.SortField);
        logger.info("提交转账的json参数为【{}】", jsonParam);
        //将转账数据提交给支付平台
        String httpPost = RestFulClient.httpPost(SystemCfg.TRANS_TOCARD_PAY_URL, jsonParam, Charsets.UTF_8.name());
        logger.info("提交转账的返回结果为【{}】", httpPost);
        //将返回数据组装成bean
        TransferRespBean transferRespBean = JSON.parseObject(httpPost, TransferRespBean.class);
        //返回的金额单位为分，转为元
        transferRespBean = amountPointToYuan(transferRespBean);
        return transferRespBean;
    }

    private TransferReqBean amountYuanToPoint(TransferReqBean trb) {
        String amount = trb.getAmount();
        if (!StringUtil.isNullOrEmpty(amount)) {
          long  amt = AmtHelper.yuanToPoint(amount);
            trb.setAmount(String.valueOf(amt));
        }
        return trb;
    }

    private static TransferRespBean amountPointToYuan(TransferRespBean transferRespBean) {
        if (null != transferRespBean) {
            String amount = transferRespBean.getAmount();
            amount = AmtHelper.poingToYuan(amount);
            transferRespBean.setAmount(amount);
        } else {//httpPost 出错时 返回null
            transferRespBean = new TransferRespBean();
            transferRespBean.setStatusCode(StatusInfo._9000.code());
            transferRespBean.setStatusDesc("网络忙，请稍后重试。");
        }
        return transferRespBean;
    }

}
